﻿@model SIG.ViewModel.Admin.LogSearchVM


@{
    ViewBag.PageTitle = "系统日志";
    ViewBag.Title = ViewBag.PageTitle + "_安全"; 

}

@section leftNav{
    @Html.Partial("_NavForSecurity")
}


<div class="main-content">
    <div class="panel panel-default">
        <header class="panel-heading">
            <span class="glyphicon glyphicon-file"></span> @ViewBag.PageTitle
        </header>

        <section class="panel-body">
            <button class="btn btn-danger delete-link pull-right" type="button" data-url="@Url.Action("Delete", new {Id = "all"})"><span class="glyphicon glyphicon-trash"></span> 清空日志</button>

            @using (Html.BeginForm("Index", "Logs", FormMethod.Get, new { @class = "form-inline" }))
            {
                <div class="form-group">
                    <label>开始时间</label>
                    @Html.TextBox("startDate", null, new { type = "date", @class = "form-control" })
                </div>
                <div class="form-group">
                    <label>结束时间</label>
                    @Html.TextBox("expireDate", null, new { type = "date", @class = "form-control" })
                </div>
                <div class="form-group">
                    <label>等级</label>
                    @Html.DropDownList("level", (IEnumerable<SelectListItem>)Model.LevelItems, new { @class = "form-control" })

                </div>
                <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span> 搜索</button>
            }


        </section>



        <table class="table table-striped">
            <tr>
                <th>
                    时间
                </th>
                <th>
                    线程
                </th>
                <th>
                    等级
                </th>
                <th>
                    日志类
                </th>
                <th>
                    消息
                </th>
                <th>
                    错误
                </th>
                <th>
                    用户名
                </th>
                <th></th>
            </tr>

            @foreach (var item in Model.Logs)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.Date)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Thread)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Level)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Logger)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Message)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Exception)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.UserName)
                    </td>
                    <td>
                        @*@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
                            @Html.ActionLink("Details", "Details", new { id=item.Id},new{@class="btn btn-primary"}) |*@
                        @*@Html.ActionLink("Delete", "Delete", new { id=item.Id }, new{@class="btn btn-danger"})*@

                        <button data-url="@Url.Action("Delete", new {Id = item.Id})" class="btn btn-xs btn-danger delete-link" type="button"><span class="glyphicon glyphicon-trash"></span></button>
                    </td>
                </tr>
            }

        </table>
        <div class="panel-footer text-center">
            @Html.PagedListPager((IPagedList)ViewBag.OnePageOfLogs, page => Url.Action("Index",
           new { page, startDate = Model.StartDate, expireDate = Model.ExpireDate, level = Model.Level }))
        </div>

    </div>
</div>



@section footer {

    <script>

        $(function () {
            //当前链接;
            Common.ActiveCurrentNav();

            //删除数据
            $('.delete-link').click(function (e) {
                var url = $(this).attr('data-url');
                var that = $(this);
                console.log(url);
                /*DELETE*/
                $.ajax({
                    url: url,
                    dataType: "json",
                    type: "POST",
                    contentType: 'application/json; charset=utf-8',
                    data: JSON.stringify({}),
                    async: true,
                    processData: false,
                    cache: false,
                    success: function (data) {
                        if (data === "one") {
                            toastr.success('日志已删除成功！', '删除日志');
                            that.closest('tr').remove();
                        }
                        else if (data === "all") {
                            toastr.success('日志已删除成功！', '删除日志');
                            setTimeout(function() {
                                location.href = "@Url.Action("Index")";
                            }, 1000);

                        }
                        else {
                            toastr.error('日志删除失败！', '删除日志');
                        }
                    },
                    error: function (xhr) {
                        toastr.error('日志删除失败！', '删除日志');
                    }
                });


                e.preventDefault();
            });

        });
    </script>
}
